package com.ruoyi.food.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResult.Type;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.food.object.TActOrder;
import com.ruoyi.food.object.TCancelVerification;
import com.ruoyi.food.object.TUser;
import com.ruoyi.food.service.TActOrderService;
import com.ruoyi.food.service.TCancelVerService;
import com.ruoyi.food.service.TUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 核销controller
 * 
 * @author mengcc
 *
 */
@Controller
@Api(value = "TCancelVerificationController", tags = "核销记录controller")
@RequestMapping("/cancel")
public class TCancelVerificationController extends BaseController {

	@Autowired
	private TCancelVerService tCancelVerService;

	@Autowired
	private TUserService tUserService;

	@Autowired
	private TActOrderService tActOrderService;

	/**
	 * 进行核销查看页面
	 * 
	 * @return
	 */
	@GetMapping()
	public String cancel() {
		return "page/cancel";
	}

	/**
	 * 查询核销信息
	 * 
	 * @param cancelVer
	 * @return
	 */
	@PostMapping("/list")
	@ApiOperation("查询核销记录列表")
	@ResponseBody
	public TableDataInfo findTActCancelVer(TCancelVerification cancelVer) {

		startPage();
		List<TCancelVerification> list = new ArrayList<>();

		list = tCancelVerService.findCancleVerByCon(cancelVer);
		
		return getDataTable(list);
	}

	@ApiOperation(value = "核销", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "mOrderId", value = "商户订单号(不可为空)", dataType = "String"),
			//@ApiImplicitParam(name = "mobile", value = "电话号码(不可为空)", dataType = "String"),
			@ApiImplicitParam(name = "enterpriseId", value = "餐饮企业id(不可为空)", dataType = "Long"),
			@ApiImplicitParam(name = "longitude", value = "经度", dataType = "String"),
			@ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String") })
	//@PostMapping("/confirm")
	@GetMapping("/confirm")
	@ResponseBody
	public AjaxResult confirmCancel(@RequestParam String mOrderId,@RequestParam long enterpriseId,@RequestParam(defaultValue = "0",required = false) String longitude,
			@RequestParam(defaultValue = "0",required = false) String latitude) {

		if(mOrderId==null||mOrderId.equals("")) {
			return new AjaxResult(Type.ERROR, "商户订单号不能为空");
		}
		
		/*if(mobile==null||mobile.equals("")) {
			return new AjaxResult(Type.ERROR, "电话号码不能为空");
		}*/

		if(enterpriseId==0) {
			return new AjaxResult(Type.ERROR, "餐饮企业id不能为空");
		}
		
		TActOrder tActOrder = tActOrderService.findTActOrderByMOrderId(mOrderId);
		
		//TUser tUser = tUserService.findUserByMobile(mobile);

		if (tActOrder != null ) {
			
			if(tActOrder.getUnionOrderNo()==null ||"".equals(tActOrder.getUnionOrderNo())) {
				return new AjaxResult(Type.ERROR, "未支付,不能进行核销");
			}
			
			if(tActOrder.getHxTime()!=null) {
				return new AjaxResult(Type.ERROR, "已核销过,不能再次核销");
			}
			
			TCancelVerification tCancelVerification = new TCancelVerification();
			tCancelVerification.setEnterpriseId(enterpriseId);
			tCancelVerification.setActId(tActOrder.getActID());
			tCancelVerification.setUserId(tActOrder.getClientId());
			tCancelVerification.setName(tActOrder.getCustomerName());
			tCancelVerification.setMoblle(tActOrder.getMobile());
			tCancelVerification.setHxType(1);
			tCancelVerification.setHxTime(new Timestamp(System.currentTimeMillis()));
			tCancelVerification.setPosNo(tActOrder.getPosNo());
			tCancelVerification.setTermNo(tActOrder.getTermNo());
			tCancelVerification.setLongitude(longitude);
			tCancelVerification.setLatitude(latitude);

			tCancelVerService.saveCancleVer(tCancelVerification,tActOrder);

			return new AjaxResult(Type.SUCCESS, "核销成功");
		}else {
			return new AjaxResult(Type.ERROR, "核销失败");
		}

	}
	@GetMapping("/list")
	@ApiOperation("查询核销记录(包含库存)，string为模糊条件，不传则查全部")	
	@ApiImplicitParam(name = "string", value = "模糊条件(可为空，不传)", dataType = "String") 
	@ResponseBody
	public List<TCancelVerification> findList(@RequestParam(defaultValue = "", required = false)String string){
		return tCancelVerService.findCancleVerByList(string);
	}

}
